//
//  ActiDataBaseHandle.m
//  UI_元旦豆瓣半完整版
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MyDataBaseHandle.h"
#import "ActivityModel.h"
#import "MovieModel.h"
#import "RegisterModel.h"

@implementation MyDataBaseHandle

+ (MyDataBaseHandle *)sharedDataBaseHandle {
    static MyDataBaseHandle *handle = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken, ^{
        handle = [[MyDataBaseHandle alloc] init];
    });
    return handle;
}
- (void)openDB {
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [document stringByAppendingPathComponent:@"Douban.sqlite"];
    NSLog(@"😈😈😈😈😈😈😈😈沙盒 : %@", dbPath);
    
    // 打开数据库
    int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"😈打开成功😈");
    } else {
        NSLog(@"打开失败");
    }
}

- (void)createTable {
    
    // sql语句
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists Activity(number integer primary key autoincrement, title text)"];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"建表成功😊");
    } else {
        NSLog(@"建表失败😰");
    }
    
    NSString *sqlStrM = [NSString stringWithFormat:@"create table if not exists Movie(number integer primary key autoincrement, original_title text)"];
    int resultM = sqlite3_exec(dbPoint, sqlStrM.UTF8String, NULL, NULL, NULL);
    
    if (resultM == SQLITE_OK) {
        NSLog(@"Movie👻建表成功");
    }
    
    NSString *sqlStrR = [NSString stringWithFormat:@"create table if not exists register(number integer primary key autoincrement, name text, key text, reKey text, Emil text, phone text)"];
    int resultR = sqlite3_exec(dbPoint, sqlStrR.UTF8String, NULL, NULL, NULL);
    
    if (resultR == SQLITE_OK) {
        NSLog(@"register👽建表成功");
    }
    
}





- (void)insertModel:(ActivityModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Activity(title) values('%@')", model.title];
    // 执行sql语句
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入失败");
    }

}

- (void)insertMovieModel:(MovieModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Movie(original_title) values('%@')", model.original_title];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"MMMMM插入成功");
    } else {
        NSLog(@"失败********");
    }
}

- (void)insertRegistModel:(RegisterModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into register(name, key, reKey, Emil, phone) values('%@', '%@', '%@', '%@', '%@')", model.name, model.key, model.reKey, model.Emil, model.phone];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"注册成功");
    }
    
}

- (NSMutableArray *)selectTable {
    NSString *sqlStr = [NSString stringWithFormat:@"select * from Activity"];
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    
    // 创建一个数组
    NSMutableArray *arrModel = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            ActivityModel *model = [ActivityModel activityModel];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            
            [arrModel addObject:model.title];
        }
    } else {
        NSLog(@"查询失败");
    }
    return arrModel;
}

- (NSMutableArray *)selectTableMovie {
    NSString *sqlStr2 = [NSString stringWithFormat:@"select * from Movie"];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, sqlStr2.UTF8String, -1, &stmt, NULL);
    
    NSMutableArray *arrmodelMovie = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            MovieModel *model = [MovieModel movieModel];
            model.original_title = [NSString stringWithUTF8String:(const char *)title];
            [arrmodelMovie addObject:model.original_title];
        }
    } else {
        NSLog(@"查询失败");
    }
    return arrmodelMovie;
}

- (NSMutableArray *)selectTableRegist {
    NSString *sqlStr = [NSString stringWithFormat:@"select * from register"];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    
    NSMutableArray *arrmodelRegist = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *key = sqlite3_column_text(stmt, 2);
            const unsigned char *reKey = sqlite3_column_text(stmt, 3);
            const unsigned char *Emil = sqlite3_column_text(stmt, 4);
            const unsigned char *phone = sqlite3_column_text(stmt, 5);
            RegisterModel *model = [RegisterModel registerModel];
            model.name = [NSString stringWithUTF8String:(const char *)name];
            model.key = [NSString stringWithUTF8String:(const char *)key];
            model.reKey = [NSString stringWithUTF8String:(const char *)reKey];
            model.Emil = [NSString stringWithUTF8String:(const char *)Emil];
            model.phone = [NSString stringWithUTF8String:(const char *)phone];
            [arrmodelRegist addObject:model];
            
        }
    } else {
        NSLog(@"登陆查询成功");
    }
    return arrmodelRegist;
}


// 删除
- (void)deleteActiModel:(ActivityModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Activity where title = '%@'", model.title];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }
}



@end
